package com.codeavatar.catools.tool;

import android.media.MediaCodec;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * +----------------------------------------------------------------------
 * | @Author: codeavatar   @Year：2022
 * +----------------------------------------------------------------------
 * | @Email: codeavatar@aliyun.com
 * +----------------------------------------------------------------------
 **/
public class CaRegexTool {
    private final String TAG = this.getClass().getSimpleName();

    //更多 (https://www.toutiao.com/article/6231678548520731137/?wid=1648278315353)
    private final String REGEX_EMAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
    private final String REGEX_CHINESE = "^[\\u4e00-\\u9fa5]{0,}$";

    //++++++++++++++++++++++++++++++++++++++
    //++ 外部方法
    //++++++++++++++++++++++++++++++++++++++

    public boolean isChinese(String chinese) {
        return regex(chinese, "^[\u4e00-\u9fa5]$");
    }

    public boolean isCellPhone(String cellphone) {
        return regex(cellphone, "^1\\d{10}$");
    }

    public boolean isNumeric(String numeric) {
        return regex(numeric, "^[0-9]*$");
    }

    public boolean isValidTagAndAlias(String s) {
        Pattern p = Pattern.compile("^[\u4E00-\u9FA50-9a-zA-Z_!@#$&*+=.|￥¥]+$");
        Matcher m = p.matcher(s);
        return m.matches();
    }

    public boolean chkRegex(String chkval,RegexType type){
        String regex = this.getRegex(type);
        return Pattern.matches(regex,chkval);
    }

    public enum RegexType{
        Email,Chinese
    }

    private static CaRegexTool init;
    public static CaRegexTool init(){
        if(null == init){
            init = new CaRegexTool();
        }
        return init;
    }

    //++++++++++++++++++++++++++++++++++++++
    //++ 内部方法
    //++++++++++++++++++++++++++++++++++++++

    private String getRegex(RegexType type){
        StringBuffer sb = new StringBuffer("");
        switch (type){
            case Email:
                sb.append(REGEX_EMAIL);
                break;
            case Chinese:
                sb.append(REGEX_CHINESE);
                break;
        }
        return sb.toString();
    }

    private boolean regex(String target, String pattern) {
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(target);
        return m.matches();
    }

    private CaRegexTool(){
//        throw new UnsupportedOperationException("Initialization is not supported");
    }
}
